/**
 * Created by lihao on 2017/6/28.
 */
var ccap = require('ccap');

exports = module.exports = function (req, res) {
	var captcha = ccap();
	//调用生成验证码的方法
	var captcha = ccap({
		width: 130,//set width,default is 256
		offset: 30,
		quality: 100,
		generate: function () {
			var items = '0123456789'.split('');
			var vcode = '';
			for (var i = 0; i < 4; i++) {
				var rnd = Math.random();
				var item = Math.round(rnd * (items.length - 1));
				vcode += item;
			}
			// return vcode;
			return '8888';
		}

	});
	var ary = captcha.get();//返回值是一个数组:ary[0] 是生成验证码图片里面的文字,ary[1] 是存储验证码图片流

	var text = ary[0];//文字
	var buffer = ary[1];//二进制流
	req.session.verifycode = text.toLowerCase();//写入session
	//把验证码流response到客户端
	res.writeHead(200, {'Content-Type': 'image/png', 'Content-Length': buffer.length});
	res.end(buffer);
};
